package com.itboye.lottery.activity;

import android.content.Intent;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableRow;
import android.widget.TextView;

import com.itboye.bylibrary.widget.glide.GlideUtil;
import com.itboye.lottery.R;
import com.itboye.lottery.activity.daletou.BascketballMainActivity;
import com.itboye.lottery.activity.football.FootballMainActivity;
import com.itboye.lottery.alert.ByAlert;
import com.itboye.lottery.base.BaseActivity;
import com.itboye.lottery.bean.OrderBean;
import com.itboye.lottery.bean.ZqBetListItem;
import com.itboye.lottery.bean.ZqMatchDataCheck;
import com.itboye.lottery.callback.PayCallBack;
import com.itboye.lottery.dialog.PayDialog;
import com.itboye.lottery.enums.LotteryType;
import com.itboye.lottery.presenter.UserPresenter;
import com.itboye.lottery.retrofit.ResponseCallback;
import com.itboye.lottery.utils.Const;
import com.itboye.lottery.utils.ConstUrl;
import com.itboye.lottery.view.rounded.RoundedImageView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import static com.itboye.lottery.utils.GsonUtil.getSkipIdAndGroupIdGson;
import static com.itboye.lottery.utils.SPUtils.getSp;

public class FaQiGenDanActivity extends BaseActivity implements ResponseCallback {

    ArrayList<ZqBetListItem> zqBetListItemArrayList = new ArrayList<>();
    private LotteryType lottery_type;
    private double single_money = 2;
    private String pass_type = "";
    private double total_money = 0;
    TableRow tr_faqigendan_content;
    RoundedImageView iv_faqigendan_user_head;
    TextView tv_faqigendan_user_name;
    String num = "";
    private int multiple;
    String passType = "";
    RadioGroup rg_faqigendan;
    EditText et_reason, et_loweast_follow_money, et_ensure_odds;
    private UserPresenter userPresenter = new UserPresenter(this);
    TextView table_title;
    int ball_type = 1;
    private ArrayList<ZqMatchDataCheck.ListEntity> arrayList=new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_fa_qi_gen_dan;
    }

    @Override
    protected void initData() {
        setTitle("发起跟单");
        zqBetListItemArrayList = (ArrayList<ZqBetListItem>) getIntent().getSerializableExtra("hash");
        lottery_type = (LotteryType) getIntent().getSerializableExtra("lottery_type");
        pass_type = getIntent().getStringExtra("pass_type");
        total_money = getIntent().getDoubleExtra("total_money", 0);
        num = getIntent().getStringExtra("num");
        multiple = getIntent().getIntExtra("multiple", 1);
        passType = getIntent().getStringExtra("passType");
        ball_type = getIntent().getIntExtra("ball_type", 1);
        /**
         *   .putExtra("hash", zqBetListItemArrayList)
         .putExtra("lottery_type", lotteryType)
         .putExtra("single_money", zqBetListItemArrayList)
         .putExtra("pass_type", buyType)
         .putExtra("total_money", voteNum * 2));
         */
        initBetData();
    }

    private void initBetData() {
        //构造投注的表格数据
        if (ball_type == 1) {
            table_title.setText("竞彩足球");
        } else if (ball_type == 2) {
            table_title.setText("竞彩篮球");
        }

        switch (lottery_type) {
            case HUNTOU:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("混合投注");
                break;
            case SPF:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("胜平负");
                break;
            case RSPF:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("让胜平负");
                break;
            case ZJQ:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("总进球");
                break;
            case BF:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("比分");
                break;
            case BQC:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("半全场");
                break;
            //篮球相关
            case SF:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("胜负");
                break;
            case RFSF:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("让胜负");
                break;
            case DXF:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("大小分");
                break;
            case SFC:
                ((TextView) tr_faqigendan_content.getChildAt(0)).setText("胜负差");
                break;
        }
        ((TextView) tr_faqigendan_content.getChildAt(1)).setText(single_money + "元");
        ((TextView) tr_faqigendan_content.getChildAt(2)).setText(pass_type.replace("*", "串"));
        ((TextView) tr_faqigendan_content.getChildAt(3)).setText("5%");
        GlideUtil.displayForCircle(this, getSp(Const.Companion.getUSER_HEAD()) + "",R.mipmap.head_default, iv_faqigendan_user_head);
        tv_faqigendan_user_name.setText(getSp(Const.Companion.getNICKNAME()) + "");
        ((RadioButton) rg_faqigendan.getChildAt(0)).setChecked(true);
    }

    @Override
    public void onClick(View view) {

    }


    public void sure(View view) {
        //先调用校验借口

        if (ball_type == 1) {
            userPresenter.getFootBallMathDataCheck(num, multiple, pass_type);//足球校验
        }else if(ball_type ==2) {
            userPresenter.getBasketballMathDataCheck(num, multiple, pass_type);//篮球检验
        }
    }

    public void rule(View view) {
        //规则说明
        startActivity(new Intent(this, WebActivity.class).putExtra("title", "规则说明").putExtra("url", ConstUrl.FOLLOW_PLAY));
    }

    @Override
    public void success(@NotNull Object o, @NotNull String requestSuccessType) {
        if (requestSuccessType == userPresenter.getFootBallMathDataCheck_SUCCESS||requestSuccessType == userPresenter.getBasketballMathDataCheck_SUCCESS) {
            ZqMatchDataCheck zqMatchDataCheck = (ZqMatchDataCheck) o;
            arrayList.clear();
            arrayList.addAll(zqMatchDataCheck.getList());
            //发起跟单
            int type = 0;
            int public_type = 0;
            String reason = et_reason.getText().toString();
            String low_amount = et_loweast_follow_money.getText().toString();
            String ensure_odds = et_ensure_odds.getText().toString();
            for (int i = 0; i < rg_faqigendan.getChildCount(); i++) {
                RadioButton radioButton = (RadioButton) rg_faqigendan.getChildAt(i);
                if (radioButton.isChecked()) {
                    public_type = Integer.parseInt(radioButton.getTag().toString());
                }
            }
            String[] passType = pass_type.split(",");
            StringBuilder stringBuilder = new StringBuilder();
            if (pass_type.equals("1*1")) {
                stringBuilder.append("1");
            }
            for (int i = 0; i < passType.length; i++) {
                String s = passType[i];
                stringBuilder.append(s.split("\\*")[0]);
                if (i != passType.length - 1) {
                    stringBuilder.append(",");
                }
            }
            String orignNum = num;
            String num = getSkipIdAndGroupIdGson().toJson(arrayList);
            userPresenter.faqigendan(ball_type, num, orignNum,multiple, pass_type, type, public_type, reason, low_amount, ensure_odds);

        } else if (requestSuccessType == userPresenter.faqigendan_SUCCESS) {
            OrderBean orderBean = (OrderBean) o;
            PayDialog payDialog = new PayDialog(this, true);
            payDialog.setOrderBean(orderBean);
            payDialog.setCallBack(new PayCallBack() {
                @Override
                public void paySuccess() {
                    ByAlert.alert("发起跟单成功");
                    if (ball_type == 1) {
                        FootballMainActivity.Companion.getActivity().clearSelctCache();
                        FootballMainActivity.Companion.getActivity().notifyData();
                    }else if (ball_type == 2) {
                        BascketballMainActivity.Companion.getActivity().clearSelctCache();
                        BascketballMainActivity.Companion.getActivity().notifyData();
                    }
                    finish();
                }

                @Override
                public void payFail(String msg) {
                    ByAlert.alert(msg);
                }
            });
            payDialog.show();
        }
    }

    @Override
    public void fail(int code, @NotNull Object msg, @NotNull String requestErrorType) {
//        if (requestErrorType == userPresenter.faqigendan_FAIL) {
        ByAlert.alert(msg);
//        }
    }
}
